package com.mebs.model.service.labor;

import javax.persistence.*;
import java.util.Date;
import java.util.Set;

import static org.hibernate.proxy.HibernateProxyHelper.getClassWithoutInitializingProxy;
import org.jt.jet.annotation.Field;
import org.jt.jet.annotation.ArgumentType;
import org.jt.jet.util.JetUtils;
import com.mebs.model.Company;

/**
 * Created by Sergey Prasolov, sap@jensentechnologies.com Date: Mar 14, 2006, 2:51:23 PM
 */
@Entity()
@Table(name = "LABOR_CONTRACT",
    uniqueConstraints = {@UniqueConstraint(columnNames = {"CompanyId", "Number"})}
)
public class Contract {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "Id")
    private Long id;

    @Column(name = "Number", nullable = false, length = 250)
    private String number;

    @Column(name = "StartDate", nullable = false)
    private Date startDate;

    @Column(name = "StopDate", nullable = true)
    private Date stopDate;

    @ManyToOne(optional = false)
    @JoinColumn(name = "CompanyId")
    @Field(type = ArgumentType.INSTANCE)
    private Company company;

    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "ContractId")
    @OrderBy("number")
    private Set<TaskOrder> taskOrders;

    public Contract()
    {
    }

    /**
     * ********************** Getters-setters ***********************
     */

    public Long getId()
    {
        return id;
    }

    public void setId(Long id)
    {
        this.id = id;
    }

    public String getNumber()
    {
        return number;
    }

    public void setNumber(String number)
    {
        this.number = number;
    }

    public Date getStartDate()
    {
        return startDate;
    }

    public void setStartDate(Date startDate)
    {
        this.startDate = startDate;
    }

    public Date getStopDate()
    {
        return stopDate;
    }

    public void setStopDate(Date stopDate)
    {
        this.stopDate = stopDate;
    }

    public Company getCompany()
    {
        return company;
    }

    public void setCompany(Company company)
    {
        this.company = company;
    }

    public Set<TaskOrder> getTaskOrders()
    {
        return taskOrders;
    }

    public void setTaskOrders(Set<TaskOrder> taskOrders)
    {
        this.taskOrders = taskOrders;
    }



    @Override
    public String toString()
    {
        return this.number;
    }

    @Transient
    public boolean isDeletable()
    {
        return JetUtils.isEmpty(this.getTaskOrders());
    }

    @Override
    public int hashCode()
    {
        if (id == null) return super.hashCode();
        return id.hashCode();
    }

    @Override
    public boolean equals(Object o)
    {
        if (o == null) return false;
        if (this == o) return true;
        if (id == null) return false;
        if (getClassWithoutInitializingProxy(this) != getClassWithoutInitializingProxy(o))
            return false;
        final Contract that = (Contract) o;
        return id.equals(that.getId());
    }

}
